<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./vue.js"></script>
</head>
<body>
  <template id="shop-item">
    <li>
      <span>{{ id }}</span>
      <b>{{ title }}</b>
      <span>{{ status | formatStatus }}</span>
      <em>{{ price | toFixed2 }}</em>
    </li>
  </template>
  <div id="app">
    <ul class="container">
      <!-- ul里只能出现li标签，所以不能直接放子组件，因此标签名还是用li，使用is属性来指定子组件 -->
      <li
        is="shopItem"
        v-for="shopItem in list"
        :key="shopItem.id"
        :title="shopItem.title"
        :status="shopItem.status"
        :price="shopItem.price"
        :id="shopItem.id"
      >
      </li>
    </ul>
  </div>
  <script>
    const shopItem = {
      template: '#shop-item',
      props: {
        title: String,
        status: {
          type: String,
          required: false,
          default: '001'
        },
        id: Number,
        price: Number
      },
      filters: {
        formatStatus (status) {
          const statusMap = {
            '001': '未付款',
            '002': '待发货',
            '003': '已发货',
            '004': '已签收',
            '005': '已评价'
          }
          return statusMap[status]
        },
        toFixed2 (num) {
          return num.toFixed(2)
        }
      }
    }

    const app = new Vue({
      el: '#app',
      data: {
        list: [
          { id: 1, status: '001', title: '拖把', price: 19.99999 },
          { id: 2, status: '004', title: '扫把', price: 9 },
          { id: 3, status: '003', title: '簸箕', price: 29.1 },
          { id: 4, status: '002', title: '吸尘器', price: 2999.159839 },
          { id: 5, status: '005', title: '鸡毛掸子', price: 23.19 }
        ]
      },
      components: {
        shopItem
      }
    })
  </script>
</body>
</html>
